package domain.regularExpression;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import domain.grammar.TerminalSymbol;

public class RegularExpression {
	private RegularExpressionElement _root;
	
	public RegularExpression(RegularExpressionElement root){
		_root = root;
	}
	
	public RegularExpressionElement getRootElement(){
		return _root;
	}
	
	
	
	public Set getSetOfElemOfClass(Class<?> aClass){
		return getSetOfElemOfClass(_root, aClass);
	}
	
	public List getListOfElemOfClass(Class<?> aClass){
		return getListOfElemOfClass(_root, aClass);
	}
	
	
	
	protected Set getSetOfElemOfClass(RegularExpressionElement ree, Class<?> aClass){
		Set result = new HashSet();
		if(aClass.isInstance(ree)){
			result.add(aClass.cast(ree));
			 
		}
		List<RegularExpressionElement> subElem = ree.getSubElements();
		Iterator<RegularExpressionElement> itSubElem = subElem.iterator();
		while(itSubElem.hasNext()){
			result.addAll(getSetOfElemOfClass(itSubElem.next(),aClass));
		}
		return result;
			
		
	}
	
	
	protected List getListOfElemOfClass(RegularExpressionElement ree, Class<?> aClass){
		List result = new Vector();
		if(aClass.isInstance(ree)){
			result.add(aClass.cast(ree));
			 
		}
		List<RegularExpressionElement> subElem = ree.getSubElements();
		Iterator<RegularExpressionElement> itSubElem = subElem.iterator();
		while(itSubElem.hasNext()){
			result.addAll(getListOfElemOfClass(itSubElem.next(),aClass));
		}
		return result;
			
		
	}
	
	
	public String toString(){
		return _root.toString();
	}
	
	
	
}
